<?php

namespace app\admin\controller\yalimedia\question;

use app\common\controller\Backend;
use app\common\model\User;
use addons\exam\model\QuestionModel;
use app\common\model\CourseQuestion;
use think\Db;

/**
 * 用户答题记录
 *
 * @icon fa fa-circle-o
 */
class Log extends Backend
{

    /**
     * Log模型对象
     * @var \app\admin\model\yalimedia\question\Log
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\yalimedia\question\Log;

    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    public function index()
    {
        $this->relationSearch = true;
        $this->searchFields = "admin.username,id";
        if ($this->request->isAjax()) {
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = $this->model
                ->alias("log")
                ->join("yalimedia_course_question_category c","log.course_id=c.id","inner")
                ->join("yalimedia_course_content q",Db::raw("q.id + 9000 = c.content_id"))
                ->join("yalimedia_course cr","cr.id=q.course_id")
                ->join("user u","log.user_id=u.id and u.deletetime is null")
                ->where($where)
                ->order('log.id', 'DESC')
                ->field('log.*,c.name as course_name,cr.title')
                ->paginate($limit);

            if($list->total() > 0){
                $user_ids  = array_unique(array_column($list->toArray()['data'],'user_id'));
                $user_list = User::where('id','IN',$user_ids)->field('id,nickname')->select();
                $user_list = array_column($user_list,'nickname','id');
            }

            foreach ($list->items() as &$value) {
                
                $value['nickname'] = $user_list[$value['user_id']];
                $value['cr.title'] = $value['title'];
                // $category = Db::name('yalimedia_course_question_category')->where('id',$value['course_id'])->field('content_id,name')->find();
                // if(empty($category)){
                //     continue;
                // }
                // $title[]  = $category['name'];
                
                // $content  = Db::name('yalimedia_course_content')->where('id',($category['content_id']-9000))->field('unit_id,title')->find();
                // if(empty($content)){
                //     continue;
                // }
                // $title[]  = $content['title'];

                // $unit     = Db::name('yalimedia_course_unit')->where('id',$content['unit_id'])->field('course_id,unit_name')->find();
                // if(empty($unit)){
                //     continue;
                // }
                // $title[]  = $unit['unit_name'];
                
                // $course   = Db::name('yalimedia_course')->where('id',$unit['course_id'])->field('title')->find();
                // if(empty($course)){
                //     continue;
                // }
                // $title[]  = $course['title'];

                // $value['course_name']  = array_reverse($title);
                // unset($title);
            }

            $result = array("total" => $list->total(), "rows" => $list->items());
            return json($result);
        }
        return $this->view->fetch();
    }
    
}
